/*     */ package com.zimbra.cs.service.util;
/*     */ 
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.CliUtil;
/*     */ import com.zimbra.cs.mailbox.MailServiceException;
/*     */ import java.io.PrintStream;
/*     */ import java.util.Enumeration;
/*     */ import junit.framework.TestCase;
/*     */ import junit.framework.TestFailure;
/*     */ import junit.framework.TestResult;
/*     */ import junit.framework.TestSuite;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class SyncToken
/*     */   implements Cloneable, Comparable
/*     */ {
/*     */   private int mChangeId;
/*  48 */   private int mOffsetInNext = -1;
/*     */   
/*     */   public SyncToken(int changeid) {
/*  51 */     assert (changeid >= 0);
/*  52 */     this.mChangeId = changeid;
/*     */   }
/*     */   
/*     */   public SyncToken(int changeid, int offsetInNextChange) {
/*  56 */     assert ((changeid >= 0) && (offsetInNextChange >= 0));
/*  57 */     this.mChangeId = changeid;
/*  58 */     this.mOffsetInNext = offsetInNextChange;
/*     */   }
/*     */   
/*     */   public SyncToken(String s) throws ServiceException {
/*  62 */     int idx = s.indexOf('-');
/*  63 */     if (idx < 0) {
/*  64 */       this.mChangeId = Integer.parseInt(s);
/*     */     } else {
/*  66 */       if (idx == s.length() - 1)
/*  67 */         throw MailServiceException.INVALID_SYNC_TOKEN(s);
/*  68 */       String lhs = s.substring(0, idx);
/*  69 */       this.mChangeId = Integer.parseInt(lhs);
/*  70 */       String rhs = s.substring(idx + 1);
/*  71 */       this.mOffsetInNext = Integer.parseInt(rhs);
/*  72 */       if (this.mOffsetInNext < 0)
/*  73 */         throw MailServiceException.INVALID_SYNC_TOKEN(s);
/*     */     }
/*     */   }
/*     */   
/*  77 */   public int getChangeId() { return this.mChangeId; }
/*  78 */   public boolean hasOffsetInNext() { return this.mOffsetInNext > 0; }
/*  79 */   public int getOffsetInNext() { return this.mOffsetInNext; }
/*     */   
/*  81 */   public String toString() { if (this.mOffsetInNext < 0) {
/*  82 */       return Integer.toString(this.mChangeId);
/*     */     }
/*  84 */     return this.mChangeId + "-" + this.mOffsetInNext;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public boolean after(int changeId)
/*     */   {
/*  95 */     return this.mChangeId >= changeId;
/*     */   }
/*     */   
/*     */   public boolean after(int changeId, int offset) {
/*  99 */     if (this.mChangeId < changeId)
/* 100 */       return false;
/* 101 */     if (this.mChangeId > changeId)
/* 102 */       return true;
/* 103 */     return this.mOffsetInNext >= offset;
/*     */   }
/*     */   
/*     */   public boolean after(SyncToken other) {
/* 107 */     if (other.mOffsetInNext >= 0) {
/* 108 */       return after(other.mChangeId, other.mOffsetInNext);
/*     */     }
/* 110 */     return after(other.mChangeId);
/*     */   }
/*     */   
/*     */   public SyncToken clone()
/*     */   {
/* 115 */     if (this.mOffsetInNext >= 0) {
/* 116 */       return new SyncToken(this.mChangeId, this.mOffsetInNext);
/*     */     }
/* 118 */     return new SyncToken(this.mChangeId);
/*     */   }
/*     */   
/*     */   public int compareTo(Object arg0) {
/* 122 */     SyncToken other = (SyncToken)arg0;
/* 123 */     int diff = this.mChangeId - other.mChangeId;
/* 124 */     if (diff == 0) {
/* 125 */       if ((this.mOffsetInNext == -1) && (other.mOffsetInNext == -1))
/* 126 */         return 0;
/* 127 */       if ((this.mOffsetInNext >= 0) && (other.mOffsetInNext == -1))
/* 128 */         return 1;
/* 129 */       if ((this.mOffsetInNext == -1) && (other.mOffsetInNext >= 0))
/* 130 */         return -1;
/* 131 */       return this.mOffsetInNext - other.mOffsetInNext;
/*     */     }
/* 133 */     return diff;
/*     */   }
/*     */   
/*     */   public static class Tester extends TestCase
/*     */   {
/*     */     public void testSyncToken() throws ServiceException
/*     */     {
/* 140 */       SyncToken one = new SyncToken(1);
/* 141 */       SyncToken two = new SyncToken(2);
/* 142 */       SyncToken three = new SyncToken(3);
/* 143 */       SyncToken two_one = new SyncToken(2, 1);
/* 144 */       SyncToken two_two = new SyncToken(2, 2);
/* 145 */       SyncToken three_one = new SyncToken(3, 1);
/*     */       
/* 147 */       assertTrue(two.after(one));
/* 148 */       assertTrue(three.after(two));
/* 149 */       assertTrue(three.after(one));
/*     */       
/* 151 */       assertFalse(one.after(three));
/*     */       
/* 153 */       assertTrue(two_one.after(two));
/* 154 */       assertFalse(two.after(two_one));
/*     */       
/* 156 */       assertTrue(two_two.after(two_one));
/* 157 */       assertFalse(two_one.after(two_two));
/*     */       
/* 159 */       assertTrue(three_one.after(three));
/* 160 */       assertTrue(three_one.after(two));
/*     */       
/* 162 */       assertFalse(three.after(three_one));
/* 163 */       assertFalse(one.after(three_one));
/*     */     }
/*     */   }
/*     */   
/*     */   public static void main(String[] args) {
/* 168 */     CliUtil.toolSetup("DEBUG");
/* 169 */     TestSuite suite = new TestSuite(Tester.class);
/* 170 */     TestResult results = new TestResult();
/* 171 */     suite.run(results);
/*     */     
/* 173 */     if (!results.wasSuccessful()) {
/* 174 */       System.out.println("\n**************************");
/* 175 */       System.out.println("TEST FAILURES:");
/* 176 */       System.out.println("**************************");
/*     */     }
/*     */     
/* 179 */     if (results.failureCount() > 0) {
/* 180 */       Enumeration failures = results.failures();
/* 181 */       while (failures.hasMoreElements()) {
/* 182 */         TestFailure error = (TestFailure)failures.nextElement();
/* 183 */         System.out.println("--> Test Failure: " + error.trace() + error.thrownException());
/* 184 */         System.out.print("\n");
/*     */       }
/*     */     }
/*     */     
/* 188 */     if (results.errorCount() > 0) {
/* 189 */       Enumeration errors = results.errors();
/* 190 */       while (errors.hasMoreElements()) {
/* 191 */         TestFailure failure = (TestFailure)errors.nextElement();
/* 192 */         System.out.println("--> Test Error: " + failure.trace() + failure.thrownException() + " at ");
/* 193 */         failure.thrownException().printStackTrace();
/* 194 */         System.out.print("\n");
/*     */       }
/*     */     }
/*     */     
/* 198 */     if (results.wasSuccessful()) {
/* 199 */       System.out.println("\n**************************");
/* 200 */       System.out.println("Tests SUCCESSFUL!");
/* 201 */       System.out.println("**************************");
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/service/util/SyncToken.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */